package com.liy.framework.common.openoffice;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.liy.framework.common.utils.Tools;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;

public class OpenOfficeTool {
    public static final String LOCAL_HOST = "10.188.10.146";
    public static final int LOCAL_PORT = 8100;
    public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();

    public OpenOfficeTool() {
    }

    public static void main(String[] args) {
        try {
            convert("C:\\Temp\\doc\\2.xls", "C:\\Temp\\doc\\8.pdf");
        } catch (ConnectException var2) {
            var2.printStackTrace();
        }

    }

    public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {
        convert(inputFilePath, outputFilePath, "10.188.10.146", 8100);
    }

    public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort) throws ConnectException {
        if (!Tools.StringIsNullOrSpace(inputFilePath) && !Tools.StringIsNullOrSpace(outputFilePath) && !Tools.StringIsNullOrSpace(connectIp)) {
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
            connection.connect();
            DocumentConverter converter = getConverter(connectIp, connection);
            converter.convert(new File(inputFilePath), new File(outputFilePath));
            connection.disconnect();
        } else {
            throw new IllegalArgumentException("参数异常！！");
        }
    }

    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream, String outputFileExtension) throws ConnectException {
        convert(inputStream, inputFileExtension, outputStream, outputFileExtension, "10.188.10.146", 8100);
    }

    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream, String outputFileExtension, String connectIp, int connectPort) throws ConnectException {
        if (inputStream != null && !Tools.StringIsNullOrSpace(inputFileExtension) && outputStream != null && !Tools.StringIsNullOrSpace(outputFileExtension) && !Tools.StringIsNullOrSpace(connectIp)) {
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
            connection.connect();
            DocumentConverter converter = getConverter(connectIp, connection);
            converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream, formatFactory.getFormatByFileExtension(outputFileExtension));
            connection.disconnect();
        } else {
            throw new IllegalArgumentException("参数异常！！");
        }
    }

    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
        DocumentConverter converter = !"localhost".equals(connectIp) && !"127.0.0.1".equals(connectIp) && !"0:0:0:0:0:0:0:1".equals(connectIp) ? new StreamOpenOfficeDocumentConverter(connection) : new OpenOfficeDocumentConverter(connection);
        return (DocumentConverter)converter;
    }
}
